package com.mender.product.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mender.product.domain.dto.SetmealDTO;
import com.mender.product.domain.dto.SetmealPageQueryDTO;
import com.mender.domain.po.Setmeal;
import com.mender.product.domain.po.SetmealDish;
import com.mender.product.domain.vo.DishItemVO;
import com.mender.product.domain.vo.SetmealVO;
import com.mender.product.mapper.SetmealDishMapper;
import com.mender.product.mapper.SetmealMapper;
import com.mender.product.service.SetmealService;
import com.mender.common.constant.MessageConstant;
import com.mender.common.constant.StatusConstant;
import com.mender.common.exception.DeletionNotAllowedException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mender.common.result.PageResult;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
//        插入套餐
        setmealMapper.save(setmeal);

        //        插入套餐菜品关系
        List<SetmealDish> setmealDishs = setmealDTO.getSetmealDishes();

        if(setmealDishs != null && setmealDishs.size()>0){
            for (SetmealDish setmealDish : setmealDishs)
                setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.save(setmealDishs);
        }

    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
//        查询套餐是否在售卖状态
        List<Setmeal> setmeals = setmealMapper.selectByIds(ids);
        for (Setmeal setmeal : setmeals) {
            if(setmeal.getStatus() == StatusConstant.ENABLE){
//                菜品在起售中,不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //        删除套餐
        setmealMapper.deleteByIds(ids);
        //        删除套餐菜品关系
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 修改套餐和关系表
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //        删除原本套餐菜品关系
        setmealDishMapper.deleteBySetmealIds(Collections.singletonList(setmeal.getId()));
        List<SetmealDish> setmealDishs = setmealDTO.getSetmealDishes();
//        插入新数据
        if(setmealDishs != null && setmealDishs.size()>0){
            for (SetmealDish setmealDish : setmealDishs)
                setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.save(setmealDishs);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealAnddishById(Long id) {
//        查询套餐
        List<Setmeal> setmeals = setmealMapper.selectByIds(Collections.singletonList(id));
        Setmeal setmeal = setmeals.get(0);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
//        查询套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 根据id查询套餐
     * @param ids
     * @return
     */
    @Override
    public List<Setmeal> selectByIds(List<Long> ids) {
        return setmealMapper.selectByIds(ids);
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public Integer countSetmealByMap(Map<String, Object> params) {
        return setmealMapper.countByMap(params);
    }
}
